/**
 * Created by KY on 2017/7/26.
 */
define('comps/validate',['comps/ajax'],function(ajax){
    var validator = (function(){
        var methods = {}
        return {
            addMethod:function(methodName,method){
                methods[methodName] = method
            },
            checkElement:function(element) {
                if(typeof element !== 'object' || element.length!==1){
                    return
                }
                var types = Array.prototype.slice.call(arguments,1);
                var promises = []
                _.each(types,function(type){
                    var validMethod = methods[type]
                    if(validMethod){
                        promises.push(validMethod(element.val(),element))
                    }
                })
                return Promise.all(promises)
            }
        }
    })()

    function isIdCardNo(num) {

        var factorArr = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
        var parityBit = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");
        var varArray = new Array();
        var intValue;
        var lngProduct = 0;
        var intCheckDigit;
        var intStrLen = num.length;
        var idNumber = num;
        // initialize
        if ((intStrLen != 15) && (intStrLen != 18)) {
            return false;
        }
        // check and set value
        for (var i = 0; i < intStrLen; i++) {
            varArray[i] = idNumber.charAt(i);
            if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {
                return false;
            } else if (i < 17) {
                varArray[i] = varArray[i] * factorArr[i];
            }
        }

        if (intStrLen == 18) {
            //check date
            var date8 = idNumber.substring(6, 14);
            if (isDate8(date8) == false) {
                return false;
            }
            // calculate the sum of the products
            for (i = 0; i < 17; i++) {
                lngProduct = lngProduct + varArray[i];
            }
            // calculate the check digit
            intCheckDigit = parityBit[lngProduct % 11];
            // check last digit
            if (varArray[17] != intCheckDigit) {
                return false;
            }
        }
        else { //length is 15
            // check date
            var date6 = idNumber.substring(6, 12);
            if (isDate6(date6) == false) {

                return false;
            }
        }
        return true;

    }
    function isDate6(sDate) {
        if(!/^[0-9]{6}$/.test(sDate)) {
            return false;
        }
        var year, month, day;
        year = sDate.substring(0, 4);
        month = sDate.substring(4, 6);
        if (year < 1700 || year > 2500) return false
        if (month < 1 || month > 12) return false
        return true
    }

    function isDate8(sDate) {
        if(!/^[0-9]{8}$/.test(sDate)) {
            return false;
        }
        var year, month, day;
        year = sDate.substring(0, 4);
        month = sDate.substring(4, 6);
        day = sDate.substring(6, 8);
        var iaMonthDays = [31,28,31,30,31,30,31,31,30,31,30,31]
        if (year < 1700 || year > 2500) return false
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1]=29;
        if (month < 1 || month > 12) return false
        if (day < 1 || day > iaMonthDays[month - 1]) return false
        return true
    }

    validator.addMethod("mobile", function (value, element) {
        var promise = new Promise(function(resolve,reject){
            var length = value.length;
            var cmRegex = /^((13[4-9])|(147)|(15[0-2,7-9])|(17[8])|(18[2-4,7-8]))\d{8}|(170[5])\d{7}$/,
                cuRegex = /^((13[0-2])|(145)|(15[5-6])|(17[156])|(18[5,6]))\d{8}|(170[4,7-9])\d{7}$/,
                ctRegex = /^((133)|(149)|(153)|(17[3,7])|(18[0,1,9]))\d{8}|(170[0-2])\d{7}$/
            if(length == 11 && (cmRegex.test(value) || cuRegex.test(value) || ctRegex.test(value))){
                resolve(true)
            }else{
                reject('请输入正确的手机号码')
            }
        })
        return promise
    })

    validator.addMethod('mobileUnique',function(value,element){
        var promise = new Promise(function(resolve,reject){
            ajax.get('queryPhoneStatus.do',{phone_no:value}).success(function(resp){
                if(!resp.resultCode){
                    if(resp.record_count!=0){
                        reject('手机号码已被使用')
                    }else{
                        resolve(true)
                    }
                }else {
                    LDFW.error({content: resp.resultMsg});
                }
            })
        })
        return promise
    })

    validator.addMethod('id_card_code',function(value,element){
        var promise = new Promise(function(resolve,reject){
            if(isIdCardNo(value)){
                ajax.post('clientCheckCredCode.do',{cred_code:value,cred_type:1,cust_type:2}).success(function(resp){
                    if(!resp.resultCode){
                        if(resp.result.record_count!=0){
                            reject('该身份证信息已录入到系统中，请联系本机构绑定登录手机')
                        }else{
                            resolve(true)
                        }
                    }else {
                        LDFW.error({content: resp.resultMsg});
                    }
                })
            }else{
                reject('身份证号码不合法')
            }
        })
        return promise
    })
    return validator
})